package com.example.tool.ui;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.Dimension;

public class SwingUi {
  public static void addChild(JPanel jPanel, JComponent jComponent, int row, int column) {
    addChild(jPanel, jComponent, row, column, false);
  }

  public static void addChild(JPanel jPanel, JComponent jComponent, int row, int column, boolean isFill) {
    if (column == 0 && !isFill) {
      jPanel.add(jComponent, new GridConstraints(row, column, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    } else if (isFill) {
      if (jComponent instanceof Spacer) {
        jPanel.add(jComponent, new GridConstraints(row, column, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_VERTICAL, GridConstraints.SIZEPOLICY_CAN_SHRINK, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
      } else {
        jPanel.add(jComponent, new GridConstraints(row, column, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
      }
    } else {
      jPanel.add(jComponent, new GridConstraints(row, column, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(300, -1), new Dimension(300, -1), 0, false));
    }
  }

  public static void addChild(JPanel jPanel, JComponent jComponent, int row) {
    jPanel.add(jComponent, new GridConstraints(row, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
  }

  @SuppressWarnings("all")
  public static boolean IsNumber(String text) {
    return text.matches("-?\\d+(\\.\\d+)?");
  }

  @SuppressWarnings("all")
  public static boolean IsEnglish(String text) {
    return text.matches("^[A-Za-z0-9]+$");
  }

  public static String removeSymbols(String text) {
    return text.replaceAll("[^a-zA-Z0-9\u4E00-\u9FA5]", "");
  }
}
